<?php
class Society_model extends MY_Model {

    function __construct() {
        parent::__construct("s_society");
    }

    /**
     * 公用查询程序
     * $condition['category'] = ?  //按照社团类别搜索
     * $condition['society_id'] = ?  //按照社团编号（社团注册人的id）
     * $condition['sort_since'] = ?  //按照社团创建时间
     * $condition['sort_view_count'] = ?  //按社团浏览次数排序
     * $condition['confirm_FLAG'] = ?  //按社团审核状态搜索
     * $condition['offset'] = ?
     * $condition['per_page'] = ?
     * @param $condition
     * @param $row_str_FLAG 仅对查询结果是单条的：若设为TRUE：单条自动转为字符串， 若设为FALSE：单条自动转为数组
     * @param $count_FLAG 若设为TRUE：计算查询结果数， 若设为FALSE：直接查询结果
     */
    function search_by_condition($condition, $row_str_FLAG = FALSE, $count_FLAG = FALSE) {
        //$this->db->select('id, society_id, s_name, abbr, since, member_num, about, web_site, contact, contact_col, contact_pro, contact_phone, contact_qq, contact_change_FLAG, new_contact, new_contact_col, new_contact_pro, new_contact_phone, new_contact_qq, confirm_FLAG, s_category, view_count, s_teacher, signin_log');

        if (isset($condition['category'])) {
            $this->db->where('s_category', $condition['category']);
        }

        if (isset($condition['society_id'])) {
            $this->db->where('society_id', $condition['society_id']);
        }
        
    	if (isset($condition['keywords'])) {
            $this->db->like('s_name', $condition['keywords']);
        }
        
    	if (isset($condition['s_name'])) {
            $this->db->where('s_name', $condition['s_name']);
        }
        
    	if (isset($condition['abbr'])) {
            $this->db->where('abbr', $condition['abbr']);
        }
        
    	if (isset($condition['s_teacher'])) {
            $this->db->where('s_teacher', $condition['s_teacher']);
        }
        
    	if (isset($condition['confirm_FLAG'])) {
            $this->db->where('confirm_FLAG', $condition['confirm_FLAG']);
        }

        if (!$count_FLAG) {
            if (isset($condition['sort_since'])) {
                $this->db->order_by("since", $condition['sort_since']);
            }
            
        	if (isset($condition['sort_view_count'])) {
    			$this->db->order_by("view_count", $condition['sort_view_count']);
    		}

            if (isset($condition['offset'])) {
                if (isset($condition['per_page'])) {
                    $this->db->limit($condition['per_page'], $condition['offset']);
                } else {
                    $this->db->limit(PER_PAGE, $condition['offset']);
                }

            }

            $query = $this->db->get('s_society');
        	if ($query->num_rows() == 1) {
				if (!$row_str_FLAG) {
	            	return $query->result_array();
				} else {
					return $query->row_array();
            	}
			} elseif ($query->num_rows() > 1) {
				return $query->result_array();
			} else {
				return NULL;
			}
        } else {
            $this->db->from('s_society');
            return $this->db->count_all_results();
        }
    }

	function update_view_count($society_id) {
		$sql = "UPDATE s_society SET view_count = view_count + 1 WHERE society_id = ?";
		$this->db->query($sql, array($society_id));
	}
	
	function update_confirm_FLAG($new_confirm_FLAG, $society_id) {
		$sql = "UPDATE s_society SET confirm_FLAG = ? WHERE society_id = ?";
		$this->db->query($sql, array($new_confirm_FLAG, $society_id));
	}
	
	function update_signin_log($new_signin_log, $society_id) {
		$sql = "UPDATE s_society SET signin_log = ? WHERE society_id = ?";
		$this->db->query($sql, array($new_signin_log, $society_id));
	}
	
	function get_signin_log($society_id) {
		$this->db->select("signin_log");
        $this->db->where("society_id", $society_id);
        $query = $this->db->get($this->table_name);
        return $query->row_array();
	}
    
    /**
     * 得到社团是否为审核通过的
     * @param $society_id
     */
    function _get_society_confirm_flag($society_id) {
        $this->db->select("confirm_FLAG");
        $this->db->where("society_id", $society_id);
        $query = $this->db->get($this->table_name);
        return $query->row_array();
    }
    
	function get_user_by_s_name($s_name) {
		$sql = "SELECT * FROM s_society WHERE s_name = ?";
		$data = $this->db->query($sql, array($s_name));
		return $data->result();
	}
	
	function get_user_by_s_abbr($abbr) {
		$sql = "SELECT * FROM s_society WHERE abbr = ?";
		$data = $this->db->query($sql, array($abbr));
		return $data->result();
	}
	
	function get_user_by_phone($phone) {
		$sql = "SELECT * FROM s_society WHERE contact_phone = ?";
		$data = $this->db->query($sql, array($phone));
		return $data->result();
	}
	
	function get_user_by_qq($qq) {
		$sql = "SELECT * FROM s_society WHERE contact_qq = ?";
		$data = $this->db->query($sql, array($qq));
		return $data->result();
	}
	
	function _get_abbr_by_society_id($society_id) {
		$this->db->select("abbr");
        $this->db->where("society_id", $society_id);
        $query = $this->db->get($this->table_name);
        $data = $query->row_array();
        return $data['abbr'];
	}
	
	function _get_full_name_by_society_id($society_id) {
		$this->db->select("s_name");
        $this->db->where("society_id", $society_id);
        $query = $this->db->get($this->table_name);
        $data = $query->row_array();
        return $data['s_name'];
	}
	
	function _get_society_id_by_confirm_FLAG($confirm_FLAG) {
		$sql = "SELECT society_id FROM s_society WHERE confirm_FLAG = ?";
		$temp_data = $this->db->query($sql, array($confirm_FLAG));
		if (count($temp_data->result_array()) != 0) {
			foreach ($temp_data->result_array() as $v) {
				$data[] = $v['society_id'];
			}
		} else {
			$data = "";
		}
		return $data;
	}

}